package com.ruoyi.project.mgcmanage.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgcmanage.domain.Medicine;
import com.ruoyi.project.mgcmanage.service.IMedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mgcmanage/medicine")
public class MedicineController extends BaseController {
    @Autowired
    private IMedicineService iMedicineService;

    /**
     * 分页查询药品信息
     * @param medicine
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:medicine:query')")
    @GetMapping("/list")
    public TableDataInfo getList(Medicine medicine){
        startPage();
        List<Medicine> list = iMedicineService.selectAll(medicine);
        return getDataTable(list);
    }

    /**
     * 添加药品信息
     * @param medicine
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:medicine:add')")
    @PostMapping
    public AjaxResult addMedicine(@RequestBody Medicine medicine){
        medicine.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iMedicineService.addMedicine(medicine));
    }

    /**
     * 做数据回显
     * @param mid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:medicine:query')")
    @RequestMapping("/{mid}")
    public AjaxResult getId(@PathVariable int mid){
        System.out.println(mid);
        return AjaxResult.success(iMedicineService.getById(mid));
    }

    /**
     * 修改药品信息
     * @param medicine
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:medicine:update')")
    @PutMapping
    public AjaxResult updateMedicine(@RequestBody Medicine medicine){
        medicine.setUpdateBy(SecurityUtils.getUsername());
        medicine.setUpdateTime(new Date());
        return toAjax(iMedicineService.updateMedicine(medicine));
    }

    /**
     * 删除药品信息
     * @param mids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:medicine:remove')")
    @DeleteMapping("/{mids}")
    public AjaxResult deleteMedicine(@PathVariable int[] mids){
        return toAjax(iMedicineService.deleteMedicine(mids));
    }

}
